
#include	"SysConfig.h"
#include	"Define.h"
#include	"Port.h"
#include	"Function.h"
#include	"Power.h"
#include	"Model_Option.h"
#ifdef  MIC
#include	"MIC.h"
#endif
#ifdef  AUTO_GUIDE
#include	"AutoGuide.h"
#endif
#ifdef  LC786XXX
#include	"LC786XXX.h"
#endif
#include	"CD_MECHA.h"
#include	"CD.h"

#ifdef  CD

extern unsigned char ucFUNC_Power;
extern CD_STATUS	ucCD_Status;
extern unsigned char ucCD_Resume;
extern unsigned char ucAUDIO_TelMute;

unsigned char ucDECK_Mode;

unsigned char ucDECK_ModeSeq;

unsigned short usDECK_Mode_Timer5msec;
unsigned char ucDECK_Direction_Timer5msec;
unsigned char ucDECK_Switch;
unsigned char ucDECK_PortLast;
unsigned char ucDECK_Disc;
unsigned char ucDECK_User_Loading;

void DECK_SetInit(void)
{
	if  (ucDECK_Mode == DECK_MODE_IDLE)
	{
		ucDECK_Mode = DECK_MODE_INIT;
		CD_DRV_MUTE_OFF;
	}
}

void DECK_SetIdle(void)
{
	ucDECK_Mode = DECK_MODE_IDLE;
	CD_DRV_MUTE_ON;
}

void DECK_FunctionChange(void)
{
#ifdef  BLUETOOTH
	if  (FUNC_GetCurrentFunction() == FUNC_HF)
	{
		return;
	}
#endif
	if  (PRW_AskACCStatusIdle() == 1)
	{
		return;
	}

	if  (ucAUDIO_TelMute == 1)
	{
		return;
	}

#ifdef  AUTO_GUIDE
	if  (AG_GetAnnounceStatus() == AG_ANNOUNCE_ON)
	{
		return;
	}
#endif
			
#ifdef  MIC
	if  (MIC_GetHostMicStatus() == MIC_ON)
	{
		MIC_SetHostMicStatus(MIC_OFF);
	}
#endif
	DISP_ClearTempDisplayMode();
	MENU_Clear();
	FUNC_FunctionChangeStart(FUNC_CD, FUNC_CHANGE_SEQ0);
}

unsigned char DECK_ReadSwitch(void)
{
	unsigned char ucTemp;

	ucTemp = 0x00;
	
	if  (PI_DECK_SW1 == 1)
	{
		ucTemp = ucTemp | 0x01;
	}
	if  (PI_DECK_SW2 == 1)
	{
		ucTemp = ucTemp | 0x02;
	}
	if  (PI_LC786_PUIN == 1)
	{
		ucTemp = ucTemp | 0x04;
	}		

//	LOG_DECK("SW:%X", ucTemp);
	if  (ucDECK_PortLast != ucTemp)
	{
		ucDECK_PortLast = ucTemp;
		return;
	}
	
	ucDECK_Switch = ucDECK_PortLast;
}

unsigned char ucDECK_MotorDirection;
unsigned char DECK_SetMotor(unsigned char ucMode)
{
	unsigned char ucRet = 1;
	
	if  (ucDECK_Direction_Timer5msec != 0)
	{
		return(0);
	}
	
	switch  (ucMode)
	{
		case DECK_MT_STOP:
			MOTOR_INPUT;
			ucDECK_Direction_Timer5msec = 50/5;
			ucDECK_MotorDirection = DECK_MT_STOP;
			break;
		case DECK_MT_LOAD:
			if  (ucDECK_MotorDirection == DECK_MT_STOP)
			{
				MOTOR_LOAD;
				MOTOR_OUTPUT;
				MOTOR_LOAD;
			}
			else
			{
				MOTOR_INPUT;
				ucDECK_MotorDirection = DECK_MT_STOP;
				ucRet = 0;
			}
			break;
		case DECK_MT_EJECT:
			if  (ucDECK_MotorDirection == DECK_MT_STOP)
			{
				MOTOR_EJECT;
				MOTOR_OUTPUT;
				MOTOR_EJECT;
			}
			else
			{
				MOTOR_INPUT;
				ucDECK_MotorDirection = DECK_MT_STOP;
				ucRet = 0;
			}
			break;
	}
	return(ucRet);
}

void DECK_SetKeepWorking(void)
{
	switch  (ucDECK_Mode)
	{
		case DECK_MODE_EJECT:
			DECK_SetMotor(DECK_MT_EJECT);
			CD_DRV_MUTE_OFF;
			break;
		case DECK_MODE_LOAD:
			DECK_SetMotor(DECK_MT_LOAD);
			CD_DRV_MUTE_OFF;
			break;
		default:
			ucDECK_Mode = DECK_MODE_IDLE;
			break;
//			if  (ucCD_Status == CD_STATUS_MOUNTED)
//			{
//				ucCD_Status = CD_STATUS_LOADED;
//			}
			break;
	}
}

void DECK_SetMode(unsigned char ucMode)
{
	ucDECK_Mode = ucMode;
	ucDECK_ModeSeq = 0;
	LOG_DECK("DECK_SetMode:%d",ucMode);
}

void DECK_Mode_Init(void)
{
	if  ((ucDECK_Switch == DECK_SW_HALFWAY)
		|| (ucDECK_Switch == DECK_SW_EJECTED))
	{
		DECK_SetMode(DECK_MODE_LOAD);
		DECK_FunctionChange();
		LOG_DECK("DECK_Mode_Init:LOAD:%X", ucDECK_Switch);
	}

	if  ((ucDECK_Switch & DECK_SW_LOADED) == DECK_SW_LOADED)
	{
		ucDECK_Disc = 1;
		ucCD_Status = CD_STATUS_LOADED;
		DECK_SetMode(DECK_MODE_NONE);
		LOG_DECK("DECK_Mode_Init:LOADED");
	}

	if  ((ucDECK_Switch == DECK_SW_EJECTED) || (ucDECK_Switch == DECK_SW_NODISC))
	{
		DECK_SetMode(DECK_MODE_NONE);
		LOG_DECK("DECK_Mode_Init:NONE");
	}
	CD_DRV_MUTE_OFF;
}

void DECK_WorkingTimeOut(void)
{
	if  (usDECK_Mode_Timer5msec == 0)
	{
		LOG_DECK("DECK_WorkingTimeOut:%d",ucDECK_Mode);
		DECK_SetMode(DECK_MODE_ERROR);
		DECK_SetMotor(DECK_MT_STOP);
	}
}

void DECK_Mode_Ejecting(void)
{
	switch  (ucDECK_ModeSeq)
	{
		case 0:
			LOG_DECK("DECK_Ejecting:Start");
			usDECK_Mode_Timer5msec = 10000/5;
			ucDECK_ModeSeq = 1;
			break;
		case 1:		// Wait Stop
		#ifdef  LC78696X
			if  ((LC786XXX_GetMediaStatus(FUNC_CD) == LC786X_MS_STOP)
			|| (LC786XXX_GetMediaStatus(FUNC_CD) == LC786X_MS_STOP_NO_TOC)
			|| (LC786XXX_GetMediaStatus(FUNC_CD) == LC786X_MS_RESET_START)
			|| (LC786XXX_GetMediaStatus(FUNC_CD) == 0x81))
			{
				if  (DECK_SetMotor(DECK_MT_EJECT) == 1)
				{
					ucDECK_ModeSeq = 2;					
					ucDECK_Disc = 0;
					CD_Variable_Initalize();
					ucCD_Status = CD_STATUS_NONE;
					LOG_DECK("DECK_Ejecting:StopOK");
				}
			}
		#endif
			break;
		case 2:
			if  (ucDECK_Switch == DECK_SW_HALFWAY)
			{
				ucDECK_ModeSeq = 3;
			}
			DECK_WorkingTimeOut();
			break;
		case 3:
			if  (ucDECK_Switch == DECK_SW_EJECTED)
			{
				LOG_DECK("DECK_Mode_Ejecting:EJECTED");
				usDECK_Mode_Timer5msec = 10/5;
				ucDECK_ModeSeq = 4;
			}
			DECK_WorkingTimeOut();
			break;
		case 4:
			if  (usDECK_Mode_Timer5msec == 0)
			{
				LOG_DECK("DECK_Mode_Ejecting:MORE");
				DECK_SetMotor(DECK_MT_STOP);
				DECK_SetMode(DECK_MODE_REMOVE);
				usDECK_Mode_Timer5msec = 10000/5;
			}
			break;
	}
}

void DECK_Mode_Loading(void)
{
	switch  (ucDECK_ModeSeq)
	{
		case 0:
			usDECK_Mode_Timer5msec = 10000/5;
			if  (DECK_SetMotor(DECK_MT_LOAD) == 1)
			{
				ucDECK_ModeSeq = 1;
				LOG_DECK("DECK_Mode_Loading:Start");
			}
			break;
		case 1:
			if  ((ucDECK_Switch & DECK_SW_LOADED) == DECK_SW_LOADED)
			{
				ucDECK_ModeSeq = 2;
				usDECK_Mode_Timer5msec = 50/5;
				DECK_SetMotor(DECK_MT_STOP);
				LOG_DECK("DECK_Mode_Loading:LOADED");
			}
			if  ((ucDECK_Switch & DECK_SW_NODISC) == DECK_SW_NODISC)
			{
				DECK_SetMode(DECK_MODE_NONE);
				DECK_SetMotor(DECK_MT_STOP);
			}
			DECK_WorkingTimeOut();
			break;
		case 2:
			ucDECK_Disc = 1;
			ucCD_Status = CD_STATUS_LOADED;
			ucDECK_Mode = DECK_MODE_NONE;
			break;
	}

}

unsigned char ucDECK_RemovalSwitch;

void DECK_Mode_RemoveWait(void)
{
	switch  (ucDECK_ModeSeq)
	{
		case 0:
			if  (ucDECK_Switch == DECK_SW_NODISC)
			{
				ucDECK_RemovalSwitch = ucDECK_Switch;
				ucDECK_ModeSeq = 1;
				LOG_DECK("DECK_RemoveWait:R");
			}
			if  (ucDECK_Switch == DECK_SW_HALFWAY)
			{
				ucDECK_RemovalSwitch = ucDECK_Switch;
				ucDECK_ModeSeq = 1;
				LOG_DECK("DECK_RemoveWait:RI");
			}
			if  (usDECK_Mode_Timer5msec == 0)
			{
				ucDECK_RemovalSwitch = ucDECK_Switch;
				ucDECK_ModeSeq = 1;
				LOG_DECK("DECK_RemoveWait:TO");
			}
			break;
		case 1:
			if  (ucDECK_RemovalSwitch == ucDECK_Switch)
			{
				if  (ucDECK_Switch == DECK_SW_NODISC)
				{
					DECK_SetMode(DECK_MODE_NONE);
					LOG_DECK("DECK_RemoveWait:Removed");
				}
				if  (ucDECK_Switch == DECK_SW_HALFWAY)
				{
					LOG_DECK("DECK_RemoveWait:ReInserted");
					DECK_SetMode(DECK_MODE_LOAD);  
					ucDECK_User_Loading = 1;
					ucCD_Status = CD_STATUS_LOADING;
					DECK_FunctionChange();
				}
				if  (usDECK_Mode_Timer5msec == 0)
				{
					LOG_DECK("DECK_RemoveWait:TimeOver");
					DECK_SetMode(DECK_MODE_LOAD);
					ucDECK_User_Loading = 0;
				}
			}
			else
			{
				ucDECK_ModeSeq = 0;
			}
			break;
		default:
			break;
	}
}

void DECK_Mode_Error(void)
{
	if  (ucDECK_Switch == DECK_SW_LOADED)
	{
		DECK_SetMode(DECK_MODE_NONE);
	}
	if  (ucDECK_Switch == DECK_SW_NODISC)
	{
		DECK_SetMode(DECK_MODE_NONE);
	}
}

void DECK_Mode_None(void)
{
	if  (ucDECK_Switch == DECK_SW_EJECTED)
	{
		DECK_SetMode(DECK_MODE_LOAD);
		ucDECK_User_Loading = 1;
		ucCD_Status = CD_STATUS_LOADING;
		DECK_FunctionChange();
		LOG_DECK("DECK_None:DET");
	}
}

unsigned char DECK_CurrentWorking(void)
{
	if  ((ucDECK_Mode == DECK_MODE_NONE) || (ucDECK_Mode == DECK_MODE_ERROR))
	{
		return(0);
	}
	return(1);
}

void DECK_Timer(void)
{
	if  (ucDECK_Direction_Timer5msec != 0)
	{
		--ucDECK_Direction_Timer5msec;
	}
	if  (usDECK_Mode_Timer5msec != 0)
	{
		--usDECK_Mode_Timer5msec;
	}
}

unsigned char DECK_CurrentMode_Eject(void)
{
	if  (ucDECK_Mode == DECK_MODE_EJECT)
	{
		return(1);
	}
	return(0);
}

unsigned char DECK_CurrentMode_Remove(void)
{
	if  (ucDECK_Mode == DECK_MODE_REMOVE)
	{
		return(1);
	}
	return(0);
}

extern unsigned char PWR_ProcessIsWakeUp(void);

void DECK_main(void)
{	
	if  (!OPT_GetOption(OPT_MASK_CD))
	{
//		ucCD_Status = CD_STATUS_NONE;
		return;
	}

	DECK_Timer();
	DECK_ReadSwitch();
		
	switch  (ucDECK_Mode)
	{
		case DECK_MODE_INIT:
			DECK_ReadSwitch();
			DECK_Mode_Init();
			break;
		case DECK_MODE_EJECT:
			DECK_Mode_Ejecting();
			break;
		case DECK_MODE_LOAD:
			DECK_Mode_Loading();
			break;
		case DECK_MODE_NONE:
			DECK_Mode_None();
			break;
		case DECK_MODE_REMOVE:
			DECK_Mode_RemoveWait();
			break;
		case DECK_MODE_ERROR:
			DECK_Mode_Error();
			break;
	}
}

#endif
